package com.gxwzxy.medic.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxwzxy.medic.entity.Area;
import com.gxwzxy.medic.entity.Family;
import com.gxwzxy.medic.service.AreaService;
import com.gxwzxy.medic.service.FamilyService;
import com.gxwzxy.medic.service.Impl.AreaServiceImpl;
import com.gxwzxy.medic.service.Impl.FamilyServiceImpl;
import com.gxwzxy.medic.until.PageBean;

/**
 *
 * Servlet implementation class FamilyServlet
 */
public class FamilyServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	FamilyService fs = new FamilyServiceImpl();
       
	/**
     * 参合家庭档案分页展示
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
	public void findFamily (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		//获取响应数据
    	String sindex = request.getParameter("index");
    	int index = 1;
		try {
    		index = Integer.parseInt(sindex);
    	}catch(NumberFormatException n) {
    		n.printStackTrace();
    	}
    	
    	PageBean<Family> pu = new PageBean<Family>();
    	pu.setIndex(index);//设置展示的当前页数
    	FamilyService as = new FamilyServiceImpl();
    	as.pageFamily(pu);
		request.setAttribute("pu", pu);
		request.getRequestDispatcher("page/family/family.jsp").forward(request, response);
	}

	/**
	 * 添加家庭档案信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addFamily(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		//获取页面数据
		String xjbh = request.getParameter("xjbh");
		String xzbh = request.getParameter("xzbh");
		String cbh = request.getParameter("cbh");
		String zbh = request.getParameter("zbh");
		String jtbh = request.getParameter("jtbh");
		String hsx = request.getParameter("hsx");
		String hzxm = request.getParameter("hzxm");
		String jtrks = request.getParameter("jtrks");
		String nyrks = request.getParameter("nyrks");
		String jtdz = request.getParameter("jtdz");
		String cjdasj = request.getParameter("cjdasj");
		String djy = request.getParameter("djy");
		
		Integer jtr = Integer.parseInt(jtrks);
		Integer nyr = Integer.parseInt(nyrks);
		
		Family family = new Family(xjbh,xzbh,cbh,zbh,jtbh,hsx,hzxm,jtr,nyr,jtdz,cjdasj,djy);
		
		int i = fs.addFamily(family);
		if(i>0) {
			request.setAttribute("url", "FamilyServlet?method=findFamily&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
	/**
	 * 删除家庭档案
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteFamily(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String jtbh = request.getParameter("jtbh");
		int i = fs.delete(jtbh);
		if(i==1) {
			response.getWriter().write("true");
		}
	}
	/**
	 * 根据jtbh查询家庭档案信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void findByFamily(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String jtbh = request.getParameter("jtbh");
		Family family = fs.findByFamily(jtbh);
		
		AreaService as = new AreaServiceImpl();
		List <Area> la = as.findAllArea();
		
		request.setAttribute("listArea", la);
		request.setAttribute("family", family);
		request.getRequestDispatcher("page/family/family_edit.jsp").forward(request, response);
	}
	
	/**
	 * 修改家庭档案信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateFamily(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		//获取页面数据
		String xjbh = request.getParameter("xjbh");
		String xzbh = request.getParameter("xzbh");
		String cbh = request.getParameter("cbh");
		String zbh = request.getParameter("zbh");
		String jtbh = request.getParameter("jtbh");
		String hsx = request.getParameter("hsx");
		String hzxm = request.getParameter("hzxm");
		String jtrks = request.getParameter("jtrks");
		String nyrks = request.getParameter("nyrks");
		String jtdz = request.getParameter("jtdz");
		String cjdasj = request.getParameter("cjdasj");
		String djy = request.getParameter("djy");
		
		Integer jtr = Integer.parseInt(jtrks);
		Integer nyr = Integer.parseInt(nyrks);
		
		Family family = new Family(xjbh,xzbh,cbh,zbh,jtbh,hsx,hzxm,jtr,nyr,jtdz,cjdasj,djy);
		int i = fs.update(family);
		if(i>0) {
			request.setAttribute("url", "FamilyServlet?method=findFamily&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
		}
	}
}
